﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Objects;


namespace PsTFS.Commandes.Add
{
    /// <summary>
    /// Ajout d'un label
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "Label")]
    public class Add_Label : PsCmdlet
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public Add_Label()
        {
            this.RecursionType = RecursionType.Full;
            this.Description = string.Empty;
            this.LabelName = string.Empty;
            this.Item = null;
        }
        /// <summary>
        /// Processus de création de label
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                if (this.Scope.IndexOf('$') < 0)
                {
                    this.Scope = "$/" + this.Scope;
                }
                //Récupération du version control de tfs 
                VersionControlServer versionControl = Methode_Service.VCS();

                //Récupération du version control pour le label
                VersionControlLabel vcl = new VersionControlLabel(versionControl
                    , this.LabelName
                    , versionControl.AuthenticatedUser
                    , this.Scope
                    , this.Description);

                //Création du tableau des item sur les quel mettre un label
                LabelItemSpec[] labelItemSpecs = new LabelItemSpec[this.Item.Length];
                for (int i = 0; i < this.Item.Length; i++)
                {
                    ItemSpec itemSpec = new ItemSpec(this.Item[i], this.RecursionType);
                    labelItemSpecs[i] = new LabelItemSpec(itemSpec, VersionSpec.Latest, false);
                }

                //Construction du label
                LabelResult[] result = versionControl.CreateLabel(vcl, labelItemSpecs, LabelChildOption.Replace);

                //Information
                Console.WriteLine("Successfully created label " + vcl.Name);

                //Renvois les informations
                WriteObject(result);
            }
            catch(Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }
        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Add-Label is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TfsTeamProjectCollection TFS
        {
            set
            {
                Context.Instance.Tfs = value;
            }
        }

        /// <summary>
        /// Nom du label
        /// </summary>
        [Parameter(Position = 1, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string LabelName { get; set; }

        /// <summary>
        /// Projet sur le quel mettre le label
        /// </summary>
        [Parameter(Position = 2)]
        public string Scope { get; set; }

        /// <summary>
        /// Description du label
        /// </summary>
        [Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Description { get; set; }

        /// <summary>
        /// Possibilité  full, OneLenvel, none
        /// </summary>
        [Parameter(Position = 4, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public RecursionType RecursionType { get; set; }

        /// <summary>
        /// Elements à labeliser
        /// </summary>
        [Parameter(Position = 5, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string[] Item { get; set; }
    }
}
